package com.mrd.gtimpframe.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.gtimpframe.entity.PlatformFee;
import com.mrd.gtimpframe.service.PlatformFeeService;
import com.mrd.util.FormBeanUtil;
import com.mrd.util.JsonBinder;
import com.mrd.util.ResponseUtils;

import org.apache.commons.lang3.StringUtils;
import com.mrd.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/platformFee")
public class PlatformFeeController {
    @Autowired
    private PlatformFeeService platformFeeService;

    /**
     * 协议计费首页
     * @author han
     * @param agentRefId
     * @param model
     * @return
     */
    @RequestMapping("listPage")
    public String listPage(String agentRefId, Model model) {
        return "/gtimpframe/orderFor/platformFee";
    }

    /**
     * 首页异步请求数据
     * @author han
     */
    @RequestMapping("getListData")
    public void getListData(Jqgrid jqgrid, HttpServletResponse response){
        String result = "";
        Pageable page = jqgrid.getPage();
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        // 分页
        PageHelper.startPage(pageNumber + 1, pageSize);

        // 排序
        Sort.Order order = jqgrid.getOrder();
        if (order != null) {
            String direction = order.getDirection().toString();
            String propertyName = order.getProperty();
            PageHelper.orderBy(propertyName + " " + direction);
        }
        PlatformFee platformFee = FormBeanUtil.getEntityFromJqgrid(jqgrid, PlatformFee.class);
        List<PlatformFee> contactList = platformFeeService.queryList(platformFee);
        PageInfo<PlatformFee> pi = new PageInfo<PlatformFee>(contactList);
        Page<Map<String, Object>> pMap = new PageImpl(contactList, page, pi.getTotal());
        JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
        jsonBinder.setDateFormat("yyyy-MM-dd");
        result = jsonBinder.toJson(pMap);
        ResponseUtils.renderJson(response, result);
    }

    /**
     * 新增修改页面
     *
     * @author dusy
     */
    @RequestMapping("editPage")
    public String editPage(String id, Model model) {
        PlatformFee platformFee = null;
        if (id != null) {
            platformFee = platformFeeService.selectByPrimaryKey(id);
        }
        model.addAttribute("platformFee", platformFee);
        return "/gtimpframe/orderFor/platformFeeEdit";
    }

    /**
     * 保存及更新协议
     *
     * @author dusy
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(PlatformFee platformFee) {
        if (StringUtils.isEmpty(platformFee.getAgentSelf()) && StringUtils.isEmpty(platformFee.getCompanyType()) && StringUtils.isEmpty(platformFee.getCustomerId()) && StringUtils.isEmpty(platformFee.getSupplierId())) {
            return Result.newFailInstance("客户名称、公司类型、是否委托自己、报关行至少填一个");
        } else {
            return platformFeeService.saveOrUpdate(platformFee);
        }
    }

    /**
     * 复制协议
     *
     * @author dusy
     */
    @RequestMapping("copyPlatformFee")
    @ResponseBody
    public Result copyPlatformFee(String id) {
        if (id != null) {
            return platformFeeService.copyPlatformFee(id);
        } else {
            return Result.newFailInstance("请刷新后操作");
        }
    }

    /**
     * 删除
     * @author han
     */
    @ResponseBody
    @RequestMapping("/delFlagBatch")
    public R delFlagBatch(@RequestBody String[] ids) {
        try {
            platformFeeService.delFlagBatch(ids);
            return R.ok();
        }catch (Exception e){
            return R.error("删除失败");
        }
    }

    /**
     * 状态确认
     * @author han
     * @param id 主键
     * @return R
     */
    @ResponseBody
    @RequestMapping("/confStatus")
    public R confStatus(String id){
        if(StringUtils.isNotEmpty(id)){
            try {
                PlatformFee platformFee = new PlatformFee();
                platformFee.setId(id);
                platformFee.setStatus("1");
                platformFeeService.confSatus(platformFee);
                return R.ok();
            }catch (Exception e){
                return R.error("删除失败");
            }
        }else{
            return R.error("主键不能为空");
        }
    }
}
